package bb.lanxing.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import bb.lanxing.R;
import bb.lanxing.model.json.RankTypeBean;
import bb.lanxing.view.FontTextView;

public class RankingsOverviewAdapter extends BaseAdapter {
    private List<RankTypeBean> rankItems = new ArrayList();
    private Context context;

    @Override
    public long getItemId(int position) {
        return position;
    }

    public RankingsOverviewAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return rankItems.size();
    }

    @Override
    public RankTypeBean getItem(int position) {
        return rankItems.get(position);
    }

    public void setRankItems(List<RankTypeBean> rankItems) {
        this.rankItems.clear();
        this.rankItems = rankItems;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(this.context, R.layout.ranking_gridview_adapter, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        RankTypeBean item = getItem(position);
        holder.rankValue.setVisibility(item.getRank() == -1 ? View.GONE : View.VISIBLE);
        holder.rankValue.setText(String.valueOf(item.getRank()));
        holder.rankIcon.setImageResource(item.getAreaType() == 0 ? R.drawable.ic_star_orange : R.drawable.ic_start_blue);
        holder.rankAddress.setText(item.getTitle());

        return convertView;
    }

    class ViewHolder {
        private TextView rankAddress;
        private ImageView rankIcon;
        private FontTextView rankValue;

        ViewHolder(View view) {
            rankIcon = view.findViewById(R.id.rankIcon);
            rankAddress = view.findViewById(R.id.rankAddress);
            rankValue = view.findViewById(R.id.rankValue);
        }
    }
}
